/*
 * Palabra.cpp
 *
 *  Created on: 13/09/2012
 *      Author: rcasal
 */

#include "Palabra.h"

namespace palabra {

Palabra::Palabra() {
}
Palabra::~Palabra() {
}

string Palabra::intToString(int entero){
	stringstream sstream;
	sstream << entero;
	return sstream.str();
}

int Palabra::stringToInt(string cadena){

	istringstream buffer(cadena);
	int value;
	buffer >> value;
	return value;
}


string Palabra::remove_tildes(char let , string cadena, int pos){

	if(let>='A' && let<='Z')
		cadena[pos]=let+32;
	else if((let==-79 || let==-111))
		cadena.replace(pos-1,2, "n" );
	//Eliminamos tildes a la a
	else if( (let==-95 || let==-127 || let==-124 || let==-92))
		cadena.replace(pos-1, 2, "a");
	//Eliminamos tildes a la e
	else if((let==-87 || let==-119 || let==-117 || let==-85))
		cadena.replace(pos-1, 2, "e");
	//Eliminamos tildes a la i
	else if((let==-83 || let==-115 || let==-113 || let==-81))
		cadena.replace(pos-1, 2, "i");
	//Eliminamos tildes a la o
	else if(  (let==-77 || let==-109 || let==-106 || let==-74))
		cadena.replace(pos-1, 2, "o");
	//Eliminamos tildes a la u
	else if( (let==-70 || let==-102 || let==-100 || let==-68))
		cadena.replace(pos-1, 2, "u");

	return cadena;
}

void Palabra::normalizar(string & palabra){
	int longitud= palabra.length();

	for (int i=0; i < longitud; i++){
			if (palabra[i] != -61){
				palabra = remove_tildes(palabra[i], palabra,i);
			}
	}
}

void Palabra::remove_character(char caract, string &cadena){
	while(cadena.find_first_of(caract) != string::npos){
		cadena.erase(cadena.find_first_of(caract), 1);
		}

}

void Palabra::remove_character_to_end(char caract, string &cadena){
	if(cadena.find_first_of(caract) != string::npos){
		cadena.erase(cadena.find_first_of(caract), cadena.length());
	}
}

list<string> Palabra::separar_cadena(string cadena){
	list<string> sin_espacios;
	list<string> sin_caracteres_invalidos;
	string letras = "abcdefghijklmnopqrstuvwxyz";
	string palabra = "";
	while(cadena.find_first_of(" ") != string::npos){
		palabra = cadena.substr(0, cadena.find_first_of(" "));
		if (!palabra.empty())
			sin_espacios.push_front(palabra);
		cadena.erase(0, cadena.find_first_of(" ") + 1);
	}
	palabra = cadena;
	if (!palabra.empty())
		sin_espacios.push_front(palabra);

	list<string>::iterator iterador;
	for(iterador = sin_espacios.begin(); iterador != sin_espacios.end(); ++iterador){
		palabra = *iterador;
		Palabra::normalizar(palabra);
		string tmp_str="";
		tmp_str.reserve(palabra.length());
		string::iterator itstring;
		for(itstring = palabra.begin(); itstring != palabra.end(); itstring++){
			if(letras.find_first_of(*itstring) != string::npos){
				tmp_str.push_back(*itstring);
			}
		}
		palabra = tmp_str;

		if(!palabra.empty()){
			sin_caracteres_invalidos.push_front(palabra);
		}
	}

	return sin_caracteres_invalidos;
}

} /* namespace palabra */
